package com.huishi.handler.thread;

import com.huishi.entity.DataSource;
import com.huishi.entity.DataXTaskInfo;
import com.huishi.entity.message.DBMsg;
import com.huishi.mapper.DataXTaskInfoMapper;
import com.huishi.ui.component.ProgressDialog;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

/**
 * 线程间数据保存器
 */
@Component
@Data
public class ThreadDataKeeper {

    @Autowired
    private DataXTaskInfoMapper dataXTaskInfoMapper;

    private Map<String, DBMsg> dbSyncMsg; // 数据同步信息
    private int totalRows; // 要同步的总记录数

    // 全量同步============================
    private Queue<String> executeSqlFile = new LinkedList<>(); // 待writer执行的sql文件(先进先出队列,保证数据顺序)
    private String sqlFilePath; // sql文件路径
    private Boolean writerRun = Boolean.TRUE; // writer运行
    private DataSource sourceDS; // 源数据源
    private String sourceDB; // 源库名
    private ProgressDialog progressDialog;// 同步ui

    // 增量同步============================
    private DataSource targetDS; // 目标数据源
    private String targetDB;

    public void addTotalRow(int rowNum){
        this.totalRows += rowNum;
    }

    public void finishTask(Long taskId){
        DataXTaskInfo dataXTaskInfo = new DataXTaskInfo();
        dataXTaskInfo.setId(taskId);
        dataXTaskInfo.setState(1);
        dataXTaskInfoMapper.updateById(dataXTaskInfo);
    }
}
